package org.MyToolsProject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class StatisticsUtil {
    private static long lineNum;
    public static void main(String[] args) {
       listFile(new File("F:\\develop\\project\\deqing-website"));
        System.out.println("项目代码总行数：" + lineNum);
    }

    public static void listFile(File file){
        if(file.isDirectory()){
            File[] files = file.listFiles();
            if(files != null && files.length > 0){
                for (int i = 0; i < files.length; i++) {
                    listFile(files[i]);
                }
            }
        }else {
            statistics(file);
        }
    }

    public static void statistics(File file){
        if(file == null || !file.getName().endsWith(".java")){
            return;
        }
        int lineCount = 0;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String line;
            while ((line = reader.readLine()) != null) {
                lineCount++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        lineNum += lineCount;
        System.out.println("文件：" + file.getAbsolutePath() + " , 代码行数：" + lineCount);
    }

}
